// pages/detail/detail.js
import { shareOptions } from "../../utils/access";
import request from "../../utils/request";
const api = require("../../config/api");
const { getDownTimeObj } = require("../../utils/utils.js");
import { getStorage } from "../../utils/utils"

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		statusMap: {
			10: ["尚未支付", "剩余支付时间 29分钟"],
			20: ["支付中", ""],
			21: ["等待买家确认完成", "确认剩余时间 5天"],
			22: ["支付失败", ""],
			31: ["订单完成", ""],
			32: ["订单完成", "买家确认完成订单超时，已自动完成"],
			41: ["交易已取消", "你已取消订单"],
			42: ["交易已取消", "订单超时未支付，已自动取消"],
		},
		goodsInfo: {},
		orderInfo: {},
		orderNo: "",
		recommendList:[] //猜你喜欢数据
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (opt) {
		this.setData({
			orderNo: opt.orderNo,
		});
		this.getDetail(opt.orderNo);
	},

	async getDetail(orderNo) {
		const res = await request.get(api.getIdleOrderDetail, { orderNo });
		const {
			data: { goodsInfoVO, orderSubStatus, ...rest },
			httpStatus = "",
		} = res;
		if (httpStatus === 200) {
			this.setData({
				goodsInfo: goodsInfoVO || {},
				orderInfo: { orderSubStatus, ...rest } || {},
			});
			orderSubStatus === 10
				? this.timeDown(rest.payEndTimeStamp, 10)
				: orderSubStatus === 21
				? this.timeDown(rest.confirmEndTimeStamp, 21)
				: "";

			// 订单状态为完成，获取推荐列表
			orderSubStatus===31 ? this.getGoodsRecommend() : ''
		}
	},

	/**
	 * 获取猜你喜欢数据
	 */
	async getGoodsRecommend(){
		let communityId= getStorage('communityId')
		let goodsClassifyId= this.data.orderInfo.goodsClassifyId
		const res= await request.get(api.recommendGoods, { 
			communityId,
      goodsClassifyId,
		}, { loading: false })
		this.setData({
			recommendList: res?.data || []
		})
	},

	handleContact() {
		this.modal.show();
	},

	/**
	 * type 10: 商品联系卖家 20: 商品联系买家
	 * flag true 当前用户为卖家
	 * */
	async confirmContact() {
		const {
			goodsInfo: { goodsId },
			orderInfo: { sourceId, targetId, flag },
		} = this.data;
		const res = await request.get(api.getContactPhone, {
			sourceId: goodsId,
			sourceUserId: sourceId,
			targetUserId: targetId,
			type: flag ? 20 : 10,
		});
		const { data = "", httpStatus = "" } = res;
		if (httpStatus === 200) {
			wx.makePhoneCall({
				phoneNumber: data,
			});
		}
	},

	// 获取 status 10 和 21 的时间表达
	getDownTimeStr(timeObj, status) {
		return status === 10
			? `剩余支付时间 ${timeObj.min}分钟`
			: Number(timeObj.day) === 0
			? `确认剩余时间 ${timeObj.hou}:${timeObj.min}`
			: `确认剩余时间 ${timeObj.day}天${timeObj.hou}:${timeObj.min}`;
	},

	timeDown(endTimeStamp, status) {
		const timeObj = getDownTimeObj(endTimeStamp);
		let {
			statusMap,
			orderInfo: { orderNo },
		} = this.data;
		statusMap[status][1] = this.getDownTimeStr(timeObj, status);
		this.setData({ statusMap });
		// 倒计时为0s
		if (!timeObj.stamp) {
			this.getDetail(orderNo);
			this.interval ? clearInterval(this.interval) : "";
			return;
		}
		const that = this;
		that.interval = setInterval(() => {
			that.interval ? clearInterval(that.interval) : "";
			that.timeDown(endTimeStamp, status);
		}, 1000 * 60);
	},

	async cancelOrder() {
		let { orderInfo } = this.data;
		const res = await request.post(api.cancelIdleOrder, {
			orderNo: orderInfo.orderNo,
		});
		const { httpStatus = "" } = res;
		if (httpStatus === 200) {
			orderInfo.orderStatus = 40;
			orderInfo.orderSubStatus = 41;
			this.setData({ orderInfo });
		}
	},

	// 买家确认完成交易
	async confirmIdleOrder() {
		this.modalOrder.show();
	},
	async confirm(){
		this.modalOrder.hide();
		let { orderInfo } = this.data;
		const res = await request.post(api.confirmIdleOrder, {
			orderNo: orderInfo.orderNo,
		});
		const { httpStatus = "" } = res;
		if (httpStatus === 200) {
			orderInfo.orderStatus = 30;
			orderInfo.orderSubStatus = 31;
			this.setData({ orderInfo });
			this.getGoodsRecommend()
		}
	},

	// 买家去支付
	handlePay() {
		// todo
		let { orderNo } = this.data;
		wx.navigateTo({
			url: "/pages/idlePay/idlePay?orderNo=" + orderNo,
		});
	},

	handleCancelOrder() {
		const that = this;
		wx.showModal({
			title: "是否确认取消",
			content: "取消后无法继续交易",
			cancelText: "暂不",
			cancelColor: "#000",
			confirmText: "确认",
			confirmColor: "#000",
			success(res) {
				if (res.confirm) {
					that.cancelOrder();
				}
			},
		});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {
		this.modal = this.selectComponent("#modal");
		this.modalOrder = this.selectComponent("#modalOrder");
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {
		clearInterval(this.interval);
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {
		const {
			orderInfo: { orderNo },
		} = this.data;
		this.getDetail(orderNo);
		wx.stopPullDownRefresh();
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {},

	// 用户点击右上角分享
	onShareAppMessage(e) {
		return shareOptions('default', e)
	},
});
